{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b4749c89",
   "metadata": {},
   "source": [
    "# 1.列表总结"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "681afad9",
   "metadata": {},
   "source": [
    "## 1.1list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ac57897",
   "metadata": {},
   "source": [
    "| names.| 说明 | 用法 |\n",
    "| :--- | :---: | ---: |\n",
    "| append | 增加在list末尾 | names.append(\"xiaoma\")|\n",
    "| insert | 添加索引,可以在指定位置增加元素 | names.insert(2,\"xiaoma\") |\n",
    "| remove | 删除元素，一般先用\"in\"判断 | if \"xiaoma\" in names: names.remove(\"xiaoma\")|\n",
    "| pop |\t删除指定位置的元素，()无内容则删除末尾元素\t| names.pop(1)|\n",
    "| index | 查看某元素在list中第一个出现的位置索引\t| print(names.index(xiaoma))|\n",
    "| clear | 清除所有数据 | names.clear()|\n",
    "| count | 查看list中元素的个数 |print(names.count(xiaoma))|\n",
    "| extend | 给指定list进行扩充\t|names.extend(stu_id)|"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9eeca786",
   "metadata": {},
   "source": [
    "## 1.2.list切片"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9678ef17",
   "metadata": {},
   "source": [
    "### data_list = [1,2,3,4,5,6]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b10514c1",
   "metadata": {},
   "source": [
    "| slice | 说明 | 用法 |取值|\n",
    "| :--- | :---: | ---: |---: |\n",
    "| [3] | 从左往右取位置为3的值 | print(data_list[2])|4|\n",
    "| [0:3] |从左往右取出位置0到2的值，最右边不取| print(data_list[0:3])|1,2,3 |\n",
    "| [:3] | 若要从0开始计算，0可以不写| print(data_list[:3])|1,2,3 |\n",
    "| [-1] | 从右往左取位置为1的值| print(data_list[-1])|6 |\n",
    "| [:] | 取所有的值|print(data_list[:])|1,2,3,4,5,6 |\n",
    "| [-3:] | 若要取最后一个值，不能写0，直接省略| print(data_list[-3:])|4,5,6 |"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "afdcde8b",
   "metadata": {},
   "source": [
    "# 2.练习"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50534344",
   "metadata": {},
   "source": [
    "## “Don't panic\" ==> \"on tap\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96278ee0",
   "metadata": {},
   "source": [
    "## 方法一：使用list的方法完成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f666316b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a']\n",
      "['o', 'n', \"'\", 't', ' ', 'p', 'a']\n",
      "['o', 'n', 't', ' ', 'p', 'a']\n",
      "['o', 'n', 't', ' ', 'a', 'p']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase) # list()可以将str转list\n",
    "print(phrase)\n",
    "print(plist)\n",
    "\n",
    "# 1.删除  n i c !\n",
    "for i in range(4):\n",
    "    plist.pop()\n",
    "print(plist)\n",
    "\n",
    "# 2. 删除 D '\n",
    "plist.remove('D')\n",
    "print(plist)\n",
    "plist.pop(2)\n",
    "print(plist)\n",
    "\n",
    "# 3.a 和 p 转换 （a+b）*（c+d）\n",
    "plist.extend([plist.pop(),plist.pop()])\n",
    "# ['o', 'n', 't', ' '] + [第一个pop a,第二个pop p]\n",
    "print(plist)\n",
    "\n",
    "# 4.t和空格 转换\n",
    "# plist.insert(2,plist.pop(3))\n",
    "# print(plist)\n",
    "# plist.pop(3) # 1.代表删除的内容的值 2.删除了plist的这个值\n",
    "# print(plist.pop(3))\n",
    "plist.insert(3,plist.pop(2))\n",
    "print(plist)\n",
    "new_phrase = ''.join(plist) # 字符串的合并\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4001aaa",
   "metadata": {},
   "source": [
    "## 方法二：使用切片的方法完成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5193371e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['o', 'n']\n",
      "on\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase) # list()可以将str转list\n",
    "print(phrase)\n",
    "print(plist)\n",
    "\n",
    "print(plist[1:3])\n",
    "on_str = ''.join(plist[1:3])\n",
    "print(on_str)\n",
    "new_phrase = on_str + plist[5] + plist[4] + plist[7] + plist[6]\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b9e51688",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
